package address;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

public class AddrList {

	static String count;

	public String getAddrCount() {
		return count;
	}

	public static int showList() {
		Connection conn = null;
		String lastName;
		String firstName;
		String mobileNumber;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager
					.getConnection(
							"jdbc:mysql://localhost:3306/jmk?useUnicode=true&characterEncoding=utf-8",
							"root", "websys");

			Statement stmt = conn.createStatement();
			Statement stmt2 = conn.createStatement();
			showList_query();
			Scanner input = new Scanner(System.in);

			ResultSet rs2 = stmt2
					.executeQuery("SELECT lastName, firstName, mobileNumber FROM address ORDER BY lastName Asc");

			while (rs2.next()) {

				lastName = rs2.getString("lastName");
				firstName = rs2.getString("firstName");
				mobileNumber = rs2.getString("mobileNumber");

				System.out.println("성:" + lastName + "");
				System.out.println("이름:" + firstName + "");
				System.out.println("핸드폰번호:" + mobileNumber + "");
				System.out.println("-------------------");

			}

			if (rs2.next() == false) {
				System.out.println("메인화면으로 돌아가려면 아무키나 눌러주세요");
				if (input.next() != null) {
					return 0;
				}
			}
			input.close();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return 0;

	}

	static void showList_query() {

		Connection conn = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager
					.getConnection(
							"jdbc:mysql://localhost:3306/jmk?useUnicode=true&characterEncoding=utf-8",
							"root", "websys");

			Statement stmt = conn.createStatement();
			Statement stmt2 = conn.createStatement();

			String lastName;
			String firstName;
			String mobileNumber;

			boolean check = true;
			ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM address");
			while (rs.next()) {

				count = rs.getString(1);

				System.out.println("전체 연락처의 개수:" + count + "");
				System.out.println("-------------------");

			}

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub

	}

	public static int deleteAll() {
		Connection conn = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager
					.getConnection(
							"jdbc:mysql://localhost:3306/jmk?useUnicode=true&characterEncoding=utf-8",
							"root", "websys");

			Statement stmt = conn.createStatement();
			Scanner input = new Scanner(System.in);

			String pw;
			String choice;

			boolean check = true;
			boolean check2 = true;

			while (check) {

				System.out.print("주소록 삭제를 위해 비밀번호를 입력하세요 : ");
				pw = input.next();

				if (PwEdit.pw_check(pw)) {
					while (check2) {

						System.out.println("정말로 삭제하시겠습니까?(Y/N)");
						choice = input.next();

						if (choice.equals("Y") || choice.equals("y")) {
							deleteall_query();
							System.out.println("주소록 전체 삭제 완료!");
							check = false;
							check2 = false;
							return 0;
						}

						else if (choice.equals("N") || choice.equals("n")) {
							return 0;
						} else
							;
					}
				} else
					;
			}

			input.close();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return 0;
	}

	static void deleteall_query() {

		Connection conn = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager
					.getConnection(
							"jdbc:mysql://localhost:3306/jmk?useUnicode=true&characterEncoding=utf-8",
							"root", "websys");

			Statement stmt = conn.createStatement();
			String create_table = "DELETE from address";
			stmt.execute(create_table);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
